package com.cn.web.dao.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.cn.web.exception.SqlProviderException;
/**
 * 1.简化sql条件拼接
 * 
 * 2.封装查询
 * 
 * @author ygc
 *
 */
public  class MysqlQuery implements Query{
	
	protected StringBuffer sql = new StringBuffer();
	
	public String getSql() {
		return sql.toString();
	}

	protected Map inMap = new HashMap();
	//参数
	protected Map paramsMap = new HashMap();
	
	public MysqlQuery() {
		sql.append(" 1 = 1 ");
	}
	
	public MysqlQuery(boolean flag) {
		if(flag){
			sql.append(" record_status = 1 ");
		}else{
			sql.append(" 1 = 1 ");
		}
	}
	
	public Query appendSql(String sqlStr){
		if( "".equals(sqlStr)){
			throw new SqlProviderException("this sql is null!");
		}
		sql.append(" and ");
		sql.append(sqlStr);
		return this;
	}
	
	@Override
	public Query setEqual(String key) {
		if( "".equals(key)){
			throw new SqlProviderException("this sql key is null!");
		}
		sql.append(" and ");
		sql.append( CamelNameUtils.camelhumpToUnderline(key) +" =#{"+key+"} ");
		return this;
	}
	
	@Override
	public Query setEqual(String key,String value) {
		if(value==null || "".equals(value)){
			return this;
		}
		sql.append(" and ");
		sql.append( CamelNameUtils.camelhumpToUnderline(key) +" =#{"+key+"} ");
		return this;
	}
	
	public Query setIn(String key,String ...values){
		if(values==null || values.length==0){
			return this;
		}
		StringBuffer sb = new StringBuffer("");
		for(String value:values){
			if(value==null || "".equals(value)){
				continue;
			}
			sb.append(value);
			sb.append(",");
		}
		String inStr = sb.toString();
		if(inStr.endsWith(",")){
			inStr = inStr.substring(0,inStr.length()-1);
		}
		if(StringUtils.isNotEmpty(inStr)){
			sql.append(" and ");
			sql.append(  CamelNameUtils.camelhumpToUnderline(key) +" in ("+inStr+") ");
			return this;
		}
		return this;
	}
	
	public Query setLike(String key,String value,String prefix,String suffix){
		if(value==null || "".equals(value)){
			return this;
		}
		if(prefix==null)prefix="";
		if(suffix==null)suffix="";
		sql.append(" and ");
		
		sql.append(  CamelNameUtils.camelhumpToUnderline(key) +" like '"+prefix+value+suffix+"' ");
		return this;
	}

	public Query setLike(String key,String prefix,String suffix){
		if(prefix==null)prefix="";
		if(suffix==null)suffix="";
		sql.append(" and ");
		//and title like CONCAT('%','${title}','%' ) 
		sql.append(  CamelNameUtils.camelhumpToUnderline(key) +" like CONCAT( '"+prefix+"','${"+key+suffix+"}','"+suffix+"') ");
		return this;
	}

	public Query setGreater(String key){
		if( "".equals(key)){
			throw new SqlProviderException("this sql key is null!");
		}
		//大于
		//and sort_index &gt;  #{sortIndex}
		sql.append(" and ");
		sql.append(  CamelNameUtils.camelhumpToUnderline(key) +" >  #{"+key+"} ");
		return this;
	}

	public Query setLess(String key){
		if( "".equals(key)){
			throw new SqlProviderException("this sql key is null!");
		}
		//小于
		//and sort_index &gt;  #{sortIndex}
		sql.append(" and ");
		sql.append(  CamelNameUtils.camelhumpToUnderline(key) +" <  #{"+key+"} ");
		return this;
	}
	
}
